<?php
class Guestbooks extends MY_Controller {
	//前置过滤
	var $beforeFilter = array(
			'initAdmin' => array(
				'only' => array('admin','add','remove','edit'),
			),
			'_init' => array(
				'only' => array('index', 'save', 'write'),
			),
		);

	function __construct()
	{
		parent::MY_Controller();
		$this->load->model('User');
		$this->load->model('Guestbook');

		$this->load->helper('url');
		$this->load->helper('system');
		$this->load->helper('form');
		$this->load->helper('cookie');
	}
	
	// 显示用户留言
	function index()
	{
		$map['where'] = array('is_shown'=>1);
		$this->config->set_item('system_per_page', 4);
		$rs = $this->_list($this->Guestbook,$map,'','*',true,'id',false);
		$this->assign['resultset'] = $rs;
		$this->display();
	}

	// 留言
	function write(){
		$this->display();
	}

	// 保存用户留言
	function save()
	{
		$redirect =  $this->getRedirect('/index.php/guestbooks/admin');
		$data = $_POST;
		if($this->Guestbook->save($data))
		{
			$this->showMessage('mes_success', $redirect);
		}
		else
		{
			$this->showMessage($this->Guestbook->_error); 
		}
	}

	// 管理用户留言
	function admin(){
		$this->config->set_item('system_per_page', 15);
		$rs = $this->_list($this->Guestbook,'','','*',true,'id',false);
		$this->assign['res'] = $rs;
		$this->display();
	}

	function view($id){
		$map['where'] = array('id'=>$id);
		$rs = $this->_list($this->Guestbook,$map,'','*',true,'id',false);

		$data['res'] = $rs;
		$this->load->view("admin/guestbooks_view", $data);
	}

	// 删除用户留言
	function remove(){
		$redirect = $this->getRedirect('/index.php/guestbooks/admin');
		$id = empty($id) ? $_REQUEST['id'] : $id;
		$this->Guestbook->removeByPks($id);
		$this->showMessage('mes_success',$redirect);
	}

	// 编辑留言信息（设置留言是否在前台显示，以及回复信息)
	function edit(){
		$redirect	=  $this->getRedirect('/index.php/guestbooks/admin');
		$data = $_POST;

		if($this->Guestbook->save($data))
		{
			$this->showMessage('mes_success', $redirect);
		}
		else
		{
			$this->showMessage($this->Guestbook->_error); 
		}
	}



}

/* End of file gusetbook.php */
/* Location: ./system/application/controllers/guestbook.php */
